$(function() {
	if(getCookieValue("token")==""){
		window.location.href = 'l-userlogin.html'
	}
	
	
	//用户积分数量
	userJifenNum({
		c_user_id: CD.geturl().business
	}, function(data) {
		if(!data.data) {
			$('.signs-right span').html(0)
		} else {
			$('.signs-right span').html(data.data.integral)
		}

	})

	//用户是否签到
	userOrSigns({
		c_user_id: CD.geturl().business
	}, function(data) {
		console.log(data.data.inte_num);
		$('.signs-add').html('+' + data.data.inte_num);

		if(data.data.is_sign == 0) {
			$('.signs-btn').on('tap', function(event) {
				userTosign({
					c_user_id: CD.geturl().business
				}, function() {
					$('.signs-add').css({
						"animation": "signsAdd 1s forwards",
					});
					$('.signs-btn').css({
						"color": "#ccc",
						"border": "2px solid #ccc"
					});

					//积分数量
					userJifenNum({
						c_user_id: CD.geturl().business
					}, function(data) {
						$('.signs-right span').html(data.data.integral)
					})
				});
			});
		} else if(data.data.is_sign == 1) {
			$('.signs-btn').css({
				"color": "#ccc",
				"border": "2px solid #ccc"
			});
		}

	})

	//明细跳转
	$('.signs-num').on('tap', function() {
		window.location.href = 'g-center-signs-jilu.html?business=' + CD.geturl().business;
	});

	//用户积分设置
	userJiFenSet(function(data) {
		$('#signs-cash').html(template('signs-cash-template', data));

		$('.cash').on('tap', function() {
			var exchangesId = $(this).attr('data-id');
			mui.confirm('是否要兑换', null, function(e) {
				if(e.index == 1) {
					//用户兑换
					userExchange({
						c_user_id: CD.geturl().business,
						exchange_id: exchangesId
					}, function(data) {
						console.log(data, 789);
						if(data.code == 200) {
							mui.toast("兑换成功！");
							//积分数量
							userJifenNum({
								c_user_id: CD.geturl().business
							}, function(data) {
								$('.signs-right span').html(data.data.integral)
							})

						} else {
							mui.toast("兑换失败！" + data.message)
						}
					});
				}
			})

		});

	});

})

//用户是否签到
var userOrSigns = function(params, callback) {
	$.ajax({
		type: "get",
		cache: false,
		traditional: true,
		dataType: "json",
		url: window.address + "/wechat/inte/getSignRecord",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: params,
		success: function(data) {
			console.log(data);
			callback && callback(data);
		}
	});
}

//用户签到接口
var userTosign = function(params, callback) {
	$.ajax({
		type: "get",
		cache: false,
		traditional: true,
		dataType: "json",
		url: window.address + "/wechat/inte/toSign",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: params,
		success: function(data) {
			console.log(data);
			callback && callback(data);
		}
	});
}

//用户积分数量
var userJifenNum = function(params, callback) {
	$.ajax({
		type: "get",
		cache: false,
		traditional: true,
		dataType: "json",
		url: window.address + "/wechat/inte/seachInte",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: params,
		success: function(data) {
			console.log(data);
			callback && callback(data);
		}
	});
}

//用户兑换设置
var userJiFenSet = function(callback) {
	$.ajax({
		type: "get",
		cache: false,
		traditional: true,
		dataType: "json",
		url: window.address + "/wechat/inte/seachExchange",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: '',
		success: function(data) {
			console.log(data);
			callback && callback(data);
		}
	});
}

//用户兑换
var userExchange = function(params, callback) {
	$.ajax({
		type: "get",
		cache: false,
		traditional: true,
		dataType: "json",
		url: window.address + "/wechat/inte/toExchange",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: params,
		success: function(data) {
			console.log(data);
			callback && callback(data);
		}
	});
}